home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / text / misc / port / string / numtostr.s < prev    next >
Encoding:
Text File  |  1996-09-07  |  2.1 KB  |  156 lines

  1.  
  2.     XDEF    NumToStr
  3.  
  4. ; Args:
  5. ; A0  Pointer to space for the converted ASCII number. (11 bytes)
  6. ; D0  32 bit number to convert.
  7.  
  8. ; Returns:
  9. ; A0  Pointer to a ASCII number string.
  10. ; D0  Length of the string.
  11.  
  12. ; A1 & D1 will be trashed.
  13.  
  14. ; I have a feeling this routine could be optimized by making it test if
  15. ; 8xNumber can be sub'ed, then 4xNumber and after that 2xNumber, and finally
  16. ; 1xNumber. This way there would be a max of four passes, before it was 10.
  17. ; It would be 4 passes against average 5 passes, so a 20% speedup?
  18.  
  19. ;    move.l    #1000000000,d0
  20. ;    move.l    #space,a0
  21. ;    bsr    NumToStr
  22. ;    rts
  23.  
  24. ;space:
  25. ;    ds.b    11
  26.  
  27. ;    even
  28.  
  29. NumToStr:
  30.     clr.l    d1
  31.     move.l    a0,a1
  32.     tst.l    d0
  33.     beq    NumToStr_Zero
  34.  
  35. NumToStr1:
  36.     cmp.l    #1000000000,d0
  37.     bcc    onegig
  38.     move.b    d1,(a0)
  39.     add.b    #"0",(a0)+
  40.     clr.l    d1
  41.  
  42. NumToStr2:
  43.     cmp.l    #100000000,d0
  44.     bcc    one100meg
  45.     move.b    d1,(a0)
  46.     add.b    #"0",(a0)+
  47.     clr.l    d1
  48.  
  49. NumToStr3:
  50.     cmp.l    #10000000,d0
  51.     bcc    one10meg
  52.     move.b    d1,(a0)
  53.     add.b    #"0",(a0)+
  54.     clr.l    d1
  55.  
  56. NumToStr4:
  57.     cmp.l    #1000000,d0
  58.     bcc    onemeg
  59.     move.b    d1,(a0)
  60.     add.b    #"0",(a0)+
  61.     clr.l    d1
  62.  
  63. NumToStr5:
  64.     cmp.l    #100000,d0
  65.     bcc    one100k
  66.     move.b    d1,(a0)
  67.     add.b    #"0",(a0)+
  68.     clr.l    d1
  69.  
  70. NumToStr6:
  71.  
  72.     divu    #10000,d0
  73.     move.b    d0,(a0)
  74.     add.b    #48,(a0)+
  75.     move.w    #0,d0
  76.  
  77.     swap    d0
  78.  
  79.     divu    #1000,d0
  80.     move.b    d0,(a0)
  81.     add.b    #48,(a0)+
  82.     move.w    #0,d0
  83.  
  84.     swap    d0
  85.  
  86.     divu    #100,d0
  87.     move.b    d0,(a0)
  88.     add.b    #48,(a0)+
  89.     move.w    #0,d0
  90.  
  91.     swap    d0
  92.  
  93.     divu    #10,d0
  94.     move.b    d0,(a0)
  95.     add.b    #48,(a0)+
  96.  
  97.     swap    d0
  98.  
  99.     move.b    d0,(a0)
  100.     add.b    #48,(a0)+
  101.  
  102.     clr.b    (a0)            ; Make it a string.
  103.     move.l    a1,a0
  104.  
  105. NumToStr11:
  106.     cmp.b    #"0",(a0)
  107.     beq    NumToStr_clearZero
  108.  
  109.     move.l    a1,a0
  110.     moveq    #10,d0
  111.  
  112. NumToStr12:
  113.     cmp.b    #" ",(a0)
  114.     beq    NumToStr_sublen
  115.     rts
  116.  
  117. NumToStr_sublen:
  118.     subq.l    #1,d0
  119.     addq.l    #1,a0
  120.     bra    NumToStr12
  121.  
  122. NumToStr_clearZero:
  123.     move.b    #" ",(a0)+
  124.     bra    NumToStr11
  125.  
  126. onegig:
  127.     sub.l    #1000000000,d0
  128.     addq.l    #1,d1
  129.     bra    NumToStr1
  130.  
  131. one100meg:
  132.     sub.l    #100000000,d0
  133.     addq.l    #1,d1
  134.     bra    NumToStr2
  135.  
  136. one10meg:
  137.     sub.l    #10000000,d0
  138.     addq.l    #1,d1
  139.     bra    NumToStr3
  140.  
  141. onemeg:
  142.     sub.l    #1000000,d0
  143.     addq.l    #1,d1
  144.     bra    NumToStr4
  145.  
  146. one100k:
  147.     sub.l    #100000,d0
  148.     addq.l    #1,d1
  149.     bra    NumToStr5
  150.  
  151. NumToStr_Zero:
  152.     move.b    #"0",(a0)
  153.     clr.b    1(a0)
  154.     moveq    #1,d0
  155.     rts
  156.